﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using P2PWeb.Models;

namespace P2PWeb.Common
{
    public static class AccountAuthorizeHelper
    {
        public static Dictionary<Type, AccountAuthorize> AccountAuthorizes = new Dictionary<Type, AccountAuthorize>();

        static AccountAuthorizeHelper()
        {
            var controllerTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(w =>
            {
                Type baseType = w;
                while (baseType != null)
                {
                    if (baseType == typeof(BaseController))
                        return true;
                    baseType = baseType.BaseType;
                }
                return false;
            });
            if (controllerTypes != null)
            {
                foreach (Type type in controllerTypes)
                {
                    AccountAuthorize authorize = type.GetCustomAttributes(false).FirstOrDefault(f => f is AccountAuthorize) as AccountAuthorize;
                    if (authorize != null)
                    {
                        AccountAuthorizes.Add(type, authorize);
                    }
                }
            }
        }
    }

    public class AccountAuthorize : Attribute
    {



        public AccountAuthorize(params AccountType[] types)
        {
            Types = types;
        }

        public AccountType[] Types { get; set; }
    }

    public class BaseController : Controller
    {
        public BaseController()
        {

        }

        //
        // GET: /Base/
        //定义一个基类的UserInfo对象

        public virtual Account CurrentUserInfo
        {
            get
            {
                return Session["UserInfo"]  as Account; //{ Response.Redirect("/Login/SignIn", true); }
            }
            set
            {
                Session["UserInfo"] = value;
            }
        }


        public static bool CanAcess(HttpContext context, AccountAuthorize accountAuthorize)
        {
            Account account = context.Session["UserInfo"] as Account;
            if (account == null)
                return false;
            if (account.Type == (int)AccountType.Admin)
                return true;
            else if (accountAuthorize != null)
            {
                if (accountAuthorize.Types.Contains((AccountType)account.Type))
                    return true;
                else
                    return false;
            }
            else
            {
                return account.Type == (int)AccountType.User;
            }
        }

        public static bool IsLogin(HttpContext context)
        {
            return context.Session["UserInfo"] != null;
        }

        public  static AccountAuthorize GetAuthorize(BaseController baseController)
        {
            if (AccountAuthorizeHelper.AccountAuthorizes.ContainsKey(baseController.GetType()))
            {
                return AccountAuthorizeHelper.AccountAuthorizes[baseController.GetType()];
            }
            return null;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            //检验用户是否已经登录，如果登录则不执行，否则则执行下面的跳转代码
            if (!CanAcess(System.Web.HttpContext.Current, GetAuthorize(this)))
            {
                Response.Redirect("/Login/SignIn?rawurl=" + Server.UrlEncode(Request.RawUrl));
            }
        }
    }
}
